<?php
/**
 * CONTENIDO extension pack (mpContenidoX), consists of multiple classes to
 * provide more features for Plugins/Modules.
 *
 * @package     mpContenidoX
 * @subpackage  Include
 * @author      Murat Purc <murat@purc.de>
 * @copyright   Copyright (c) 2008-2012 Murat Purc (http://www.purc.de)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
 * @version     $Id: mpInclude.class.php 21 2012-07-16 21:33:14Z purcaholic $
 */


defined('CON_FRAMEWORK') or die('Illegal call');


/**
 * Source include class
 *
 * @package     mpContenidoX
 * @subpackage  Include
 * @author      Murat Purc <murat@purc.de>
 */
class mpInclude
{

    /**
     * Function to include files from contenido/includes/ directory.
     *
     * @param   $what   string|array  Either file or list of filenames to include
     * @param   $force  boolean       Flag to force inclusion, used on include a file several times
     * @return  void
     */
    public static function inc($what, $force=false)
    {
        self::_include('includes', $what, $force=false);
    }


    /**
     * Function to include classes from contenido/classes/ directory.
     *
     * @param   $what   string|array  Either file or list of filenames to include
     * @param   $force  boolean       Flag to force inclusion, used on include a file several times
     * @return  void
     */
    public static function classes($what, $force=false)
    {
        self::_include('classes', $what, $force=false);
    }


    /**
     * Function to include sources from frontend (see client path).
     *
     * @param   $what   string|array  Either file or list of filenames to include
     * @param   $force  boolean       Flag to force inclusion, used on include a file several times
     * @return  void
     */
    public static function frontend($what, $force=false)
    {
        self::_include('frontend', $what, $force=false);
    }


    /**
     * Function to include sources from configured wysiwyg directory, contenido/external/wysiwyg/{editor}/.
     *
     * @param   $what   string|array  Either file or list of filenames to include
     * @param   $force  boolean       Flag to force inclusion, used on include a file several times
     * @return  void
     */
    public static function wysiwyg($what, $force=false)
    {
        self::_include('wysiwyg', $what, $force=false);
    }


    /**
     * Function to include sources from contenido/external/wysiwyg/ directory.
     *
     * @param   $what   string|array  Either file or list of filenames to include
     * @param   $force  boolean       Flag to force inclusion, used on include a file several times
     * @return  void
     */
    public static function allWysiwyg($what, $force=false)
    {
        self::_include('all_wysiwyg', $what, $force=false);
    }


    /**
     * Function to include sources from conlib/ directory.
     *
     * @param   $what   string|array  Either file or list of filenames to include
     * @param   $force  boolean       Flag to force inclusion, used on include a file several times
     * @return  void
     */
    public static function conlib($what, $force=false)
    {
        self::_include('conlib', $what, $force=false);
    }


    /**
     * Function to include sources from pear/ directory.
     *
     * @param   $what   string|array  Either file or list of filenames to include
     * @param   $force  boolean       Flag to force inclusion, used on include a file several times
     * @return  void
     */
    public static function pear($what, $force=false)
    {
        self::_include('pear', $what, $force=false);
    }


    /**
     * Wrapper for @see cInclude() function. Provides also inclusion of passed file list.
     *
     * @param   $where  string        Area which should be included
     * @param   $what   string|array  Either file or list of filenames to include
     * @param   $force  boolean       Flag to force inclusion, used on include a file several times
     * @return  void
     */
    private static function _include($where, $what, $force=false)
    {
        if (!is_array($what)) {
            $what = array($what);
        }
        foreach ($what as $item) {
            cInclude($where, $item, $force);
        }
    }

    /**
     * Wrapper for @see plugin_include() function. Provides also inclusion of passed file list.
     *
     * You can use abbreviated form of file to include as follows:
     * - class.myName.php (prefix "class.") to include class.myName.php from classes/ subfolder
     * - include.myName.php (prefix "include.") to include include.myName.php from includes/ subfolder
     *
     * @param   $where  string        Name of plugin
     * @param   $what   string|array  Either file or list of filenames to include
     * @return  void
     */
    public static function plugin($name, $what)
    {
        if (!is_array($what)) {
            $what = array($what);
        }
        foreach ($what as $item) {
            if (strpos($item, 'class.') === 0) {
                $item = 'classes/' . $item;
            } elseif (strpos($item, 'include.') === 0) {
                $item = 'includes/' . $item;
            }
            plugin_include($name, $item);
        }
    }


}
